package io.renren.modules.api.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.api.interceptor.AuthorizationInterceptor;
import io.renren.modules.collect.entity.CollectinfosCustomEntity;
import io.renren.modules.collect.entity.CollectinfosEntity;
import io.renren.modules.collect.service.CollectinfosService;
import io.renren.modules.projectinfo.service.ProjectinfosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: stf
 * @description: 收藏
 * @author: Custom
 * @create: 2018-11-01 21:57
 **/
@RestController
@RequestMapping("/api/collect")
public class ApiCollectController {

    @Autowired
    private CollectinfosService collectinfosService;

    @Autowired
    private ProjectinfosService projectinfosService;

    /**
     * 收藏状态
     */
    @PostMapping("/state")
    public R state(String projectid, HttpServletRequest request){
        if(projectid == null){
            return R.error("参数不能为空！");
        }
        //获取用户id
        String userinfoid = (String) request.getAttribute(AuthorizationInterceptor.USER_KEY);
        CollectinfosEntity collectinfosEntity = new CollectinfosEntity();
        collectinfosEntity.setUserinfoid(userinfoid);
        collectinfosEntity.setTargetid(projectid);
        if(collectinfosService.collectState(collectinfosEntity)){
            return R.ok().put("state",1);
        }
        return R.ok().put("state",0);
    }

    //获取收藏列表
    @GetMapping("/list")
    public R submitFeedback(@RequestParam Map<String, Object> params, HttpServletRequest request){
        if(params == null){
            return R.error("不能为空哦！");
        }
        //获取用户
        String userinfoid = (String) request.getAttribute(AuthorizationInterceptor.USER_KEY);
        params.put("userinfoid",userinfoid);
        //查询列表数据
        Query query = new Query(params);
        List<CollectinfosCustomEntity> collectinfosCustomEntities = collectinfosService.apiCollectList(query);
        int total = collectinfosService.queryApiTotal(query);
        PageUtils pageUtil = new PageUtils(collectinfosCustomEntities, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    //收藏项目
    @PostMapping("/add")
    public R collectProject(String projectinfoid, HttpServletRequest request){
        if(projectinfoid == null){
            return R.error("不能为空哦！");
        }
        //获取用户id
        String userinfoid = (String) request.getAttribute(AuthorizationInterceptor.USER_KEY);
        if(userinfoid == null){
            return R.error("请重新登陆再尝试");
        }
        CollectinfosEntity collectinfosEntity = new CollectinfosEntity();
        collectinfosEntity.setUserinfoid(userinfoid);
        collectinfosEntity.setTargetid(projectinfoid);
        //判断用户和项目是否存在
        R existCollect = collectinfosService.isExistCollect(collectinfosEntity);
        if(existCollect != null){
            return existCollect;
        }
        R auditSucceed = projectinfosService.judgeProjectAuditSucceed(projectinfoid);
        if(auditSucceed != null){
            return auditSucceed;
        }
        //添加收藏
        collectinfosService.save(collectinfosEntity);
        //添加收藏次数
        projectinfosService.addCollectTimes(projectinfoid);
        return R.ok().put("msg","收藏成功!");
    }

    //取消收藏（单个）
    @PostMapping("/delete")
    public R deleteCollect(String targetid, HttpServletRequest request){
        if(targetid == null){
            return R.error("不能为空哦！");
        }
        String userinfoid = (String) request.getAttribute(AuthorizationInterceptor.USER_KEY);
        Map<String,Object> map = new HashMap<>(16);
        map.put("userinfoid",userinfoid);
        map.put("targetid",targetid);
        int i = collectinfosService.apiDelete(map);
        if(i == 0){
            return R.error("收藏不存在，删除失败！");
        }
        return R.ok().put("msg",i+"条,删除成功！");
    }

    //取消收藏（多个）
    @PostMapping("/deletes")
    public R deleteCollects(String targetids, HttpServletRequest request){
        String[] split = targetids.split(",");
        String userinfoid = (String) request.getAttribute(AuthorizationInterceptor.USER_KEY);
        Map<String,Object> map = new HashMap<>(16);
        map.put("userinfoid",userinfoid);
        map.put("targetids",split);
        int i = collectinfosService.apiDeleteBatch(map);
        if(i == 0){
            return R.error("收藏不存在,删除失败！");
        }
        return R.ok().put("msg","成功删除"+i+"条！");
    }

}
